package com.eqdd.yiqidian.ui.companyspace.base

import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.kotlin.deleteDialog
import com.eqdd.yiqidian.kotlin.net.HttpResult
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.AbsCallback
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_space_html_info.*
import org.jetbrains.anko.toast
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

/**
 * Created by jhj on 18-6-27.
 */
abstract class BaseSpaceHtmlInfoActivity<T : ISpaceHtmlBean<T>> : BaseActivity() {

    abstract val url: String
    abstract val title: String
    abstract val deleteUrl: String?
    abstract val deleteFieldName: String


    private var bean: T? = null

    override fun initBinding() {
        setContentView(R.layout.activity_space_html_info)
        richEditor.setInputEnabled(false)
        richEditor.setPadding(15, 0, 15, 15)
        initTopTitleBar(View.VISIBLE, title)
        deleteUrl?.let { url ->
            initTopRightText("删除") {
                deleteDialog(url, "是否删除${bean?.title ?: "该数据"}", deleteFieldName to bean?.id)
            }
        }


    }

    override fun initData() {
        OkGo.post<HttpResult<T>>(url)
                .params("companyId", user.companyId)
                .params("userGuid", user.guid)
                .params(requestParams())
                .execute(object : AbsCallback<HttpResult<T>>() {
                    override fun onSuccess(response: Response<HttpResult<T>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            bean = result.items
                            val title = if (bean?.title?.isNotBlank() == true) {
                                "<h3 align=\"center\">${bean?.title}</h3><br>"
                            } else {
                                ""
                            }
                            val html = (title + bean?.content)
                                    .replace("<pre>", "")
                                    .replace("</pre>", "")
                                    .replace("<br>", "")
                            richEditor.html = html
                        } else {
                            toast("获取详情失败")
                            finish()
                        }
                    }

                    override fun convertResponse(response: okhttp3.Response?): HttpResult<T>? {
                        val str = response?.body()?.string()
                        var result: HttpResult<T>? = null
                        try {
                            result = HttpResult<T>().parseJson(str, getTClazz())
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                        return result
                    }

                })
    }

    abstract fun requestParams(): HttpParams

    override fun setView() {

    }


    fun getTClazz(): Type {
        //获取当前类带有泛型的父类
        val clazz = this.javaClass.genericSuperclass
        return if (clazz is ParameterizedType) {
            //获取父类的泛型参数（参数可能有多个，获取第一个）
            clazz.actualTypeArguments[0]
        } else {
            clazz
        }
    }

}